<template>
	<view class="detail-container">
		<view class="ticket-header">
			<text class="ticket-title">{{ ticket.title }}</text>
			<text class="ticket-status" :class="ticket.status">{{ getStatusText(ticket.status) }}</text>
		</view>
		
		<view class="ticket-info">
			<view class="info-item">
				<text class="placeholder-icon"></text>
				<text class="label">工单编号：</text>
				<text class="value">{{ ticket.id }}</text>
			</view>
			<view class="info-item">
				<text class="placeholder-icon"></text>
				<text class="label">创建时间：</text>
				<text class="value">{{ ticket.createTime }}</text>
			</view>
			<view class="info-item" v-if="ticket.address">
				<text class="placeholder-icon"></text>
				<text class="label">地址：</text>
				<text class="value">{{ ticket.address }}</text>
			</view>
			<view class="info-item">
				<text class="placeholder-icon"></text>
				<text class="label">优先级：</text>
				<text class="value" :class="ticket.priority">{{ getPriorityText(ticket.priority) }}</text>
			</view>
			<view class="info-item">
				<text class="placeholder-icon"></text>
				<text class="label">状态：</text>
				<text class="value" :class="ticket.status">{{ getStatusText(ticket.status) }}</text>
			</view>
		</view>
		
		<view class="ticket-description">
			<text class="section-title">
				<text class="placeholder-icon"></text>
				问题描述
			</text>
			<view class="description-content">
				<text>{{ ticket.description }}</text>
			</view>
		</view>
		
		<view class="ticket-process">
			<text class="section-title">
				<text class="placeholder-icon"></text>
				处理记录
			</text>
			<view class="process-list">
				<view class="process-item" v-for="(record, index) in ticket.processRecords" :key="index">
					<view class="process-header">
						<text class="processor">{{ record.processor }}</text>
						<text class="process-time">{{ record.time }}</text>
					</view>
					<view class="process-content">
						<text>{{ record.content }}</text>
					</view>
				</view>
				
				<view class="no-records" v-if="!ticket.processRecords || ticket.processRecords.length === 0">
					<text class="iconfont icon-empty empty-icon"></text>
					<view>暂无处理记录</view>
				</view>
			</view>
		</view>
		
		<view class="action-buttons">
			<button class="action-btn complete-btn" @click="completeTicket" v-if="ticket.status === 'processing'">
				<text class="iconfont icon-check"></text>
				<text>完成工单</text>
			</button>
			<button class="action-btn process-btn" @click="processTicket" v-if="ticket.status === 'processing'">
				<text class="iconfont icon-edit"></text>
				<text>处理工单</text>
			</button>
		</view>
	</view>
</template>

<script>
	import request from '../../../utils/request.js';
	export default {
		data() {
			return {
				ticketId: null,
				ticket: {
					id: 1,
					title: '',
					description: '',
					status: 'processing',
					priority: 'medium',
					createTime: '',
					processRecords: []
				}
			}
		},
		onLoad(options) {
			if (options.id) {
				this.ticketId = options.id;
				this.loadTicketDetail(options.id);
			}
		},
		methods: {
			loadTicketDetail(id) {
				request({
					url: `/api/tickets/${id}`,
					method: 'POST',
					data: { id }
				}).then(res => {
					if (res.code === 200) {
						this.ticket = res.data;
					} else {
						uni.showToast({
							title: res.message || '获取工单详情失败',
							icon: 'none'
						});
					}
				}).catch(err => {
					console.error('获取工单详情失败', err);
					uni.showToast({
						title: '网络请求失败',
						icon: 'none'
					});
				});
			},
			getStatusText(status) {
				const statusMap = {
					'processing': '处理中',
					'completed': '已完成'
				};
				return statusMap[status] || '未知';
			},
			getPriorityText(priority) {
				const priorityMap = {
					'low': '低优先级',
					'medium': '中优先级',
					'high': '高优先级'
				};
				return priorityMap[priority] || '未知';
			},
			completeTicket() {
				// 完成工单
				request({
					url: `/api/tickets/${this.ticketId}/complete`,
					method: 'POST'
				}).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: '工单已完成',
							icon: 'success'
						});
						
						// 更新工单状态
						this.ticket.status = 'completed';
					} else {
						uni.showToast({
							title: res.message || '完成工单失败',
							icon: 'none'
						});
					}
				}).catch(err => {
					console.error('完成工单失败', err);
					uni.showToast({
						title: '网络请求失败',
						icon: 'none'
					});
				});
			},
			processTicket() {
				// 处理工单
				console.log('处理工单');
				uni.navigateTo({
					url: '/pages/ticket/process?id=' + this.ticketId
				});
			}
		}
	}
</script>

<style scoped>
	.detail-container {
		padding: 20rpx;
	}
	
	.ticket-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: white;
		padding: 30rpx;
		border-radius: 10rpx;
		margin-bottom: 20rpx;
	}
	
	.ticket-title {
		font-size: 36rpx;
		font-weight: bold;
	}
	
	.ticket-status {
		font-size: 24rpx;
		padding: 5rpx 15rpx;
		border-radius: 5rpx;
	}
	
	.ticket-status.processing {
		background-color: #ffeee0;
		color: #ff6b00;
	}
	
	.ticket-status.completed {
		background-color: #e8f8f5;
		color: #00b894;
	}
	
	.ticket-info {
		background-color: white;
		padding: 30rpx;
		border-radius: 10rpx;
		margin-bottom: 20rpx;
	}
	
	.info-item {
		display: flex;
		margin-bottom: 15rpx;
	}
	
	.info-item:last-child {
		margin-bottom: 0;
	}
	
	.label {
		width: 150rpx;
		color: #999;
	}
	
	.value {
		flex: 1;
	}
	
	.value.high {
		color: #c62828;
	}
	
	.value.medium {
		color: #ff8f00;
	}
	
	.value.low {
		color: #1e88e5;
	}
	
	.ticket-description, .ticket-process {
		background-color: white;
		padding: 30rpx;
		border-radius: 10rpx;
		margin-bottom: 20rpx;
	}
	
	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
		display: block;
	}
	
	.description-content {
		color: #666;
		line-height: 1.6;
	}
	
	.process-list {
		min-height: 100rpx;
	}
	
	.process-item {
		border-left: 2rpx solid #007AFF;
		padding-left: 20rpx;
		margin-bottom: 30rpx;
		position: relative;
	}
	
	.process-item:before {
		content: '';
		width: 20rpx;
		height: 20rpx;
		border-radius: 50%;
		background-color: #007AFF;
		position: absolute;
		left: -11rpx;
		top: 5rpx;
	}
	
	.process-header {
		display: flex;
		justify-content: space-between;
		margin-bottom: 10rpx;
	}
	
	.processor {
		font-weight: bold;
	}
	
	.process-time {
		color: #999;
	}
	
	.process-content {
		color: #666;
		line-height: 1.6;
	}
	
	.no-records {
		text-align: center;
		padding: 30rpx 0;
		color: #999;
	}
	
	.action-buttons {
		display: flex;
		justify-content: space-between;
		padding: 20rpx 0;
	}
	
	.action-btn {
		flex: 1;
		margin: 0 10rpx;
		border: none;
		padding: 20rpx 0;
		border-radius: 10rpx;
		font-size: 32rpx;
	}
	
	.complete-btn {
		background-color: #00b894;
		color: white;
	}
	
	.process-btn {
		background-color: #007AFF;
		color: white;
	}
	
	.info-icon {
		font-size: 36rpx;
		margin-right: 10rpx;
		color: #007AFF;
	}
	
	.title-icon {
		font-size: 36rpx;
		margin-right: 10rpx;
		color: #007AFF;
	}
	
	.empty-icon {
		font-size: 100rpx;
		color: #ccc;
		margin-bottom: 20rpx;
	}
</style>